﻿using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using System.Reflection;

namespace Ooaavee.Data.Model
{
    /// <summary>
    /// A relational model of data.
    /// </summary>
    [DataContract]
    [DebuggerDisplay("{DebuggerDisplay,nq}")]
    public sealed class RelationalModel : ITableFinder
    {
        [DataMember]
        private readonly TableCollection _tables = new TableCollection();

        /// <summary>
        /// All tables in the database.
        /// </summary>
        public TableCollection Tables
        {
            get { return _tables; }
        }

        /// <summary>
        /// Database name.
        /// </summary>
        [DataMember]
        public string DatabaseName { set; get; }

        /// <summary>
        /// Gets all system tables in this relational model.
        /// </summary>
        public ReadOnlyTableCollection SystemTables
        {
            get { return TableCollection.FindByTableType(Tables, Table.TableTypeSystemTable); }
        }

        /// <summary>
        /// Gets all system views in this relational model.
        /// </summary>
        public ReadOnlyTableCollection SystemViews
        {
            get { return TableCollection.FindByTableType(Tables, Table.TableTypeSystemView); }
        }

        /// <summary>
        /// Gets all user tables in this relational model.
        /// </summary>
        public ReadOnlyTableCollection UserTables
        {
            get { return TableCollection.FindByTableType(Tables, Table.TableTypeUserTable); }
        }

        /// <summary>
        /// Gets all user views in this relational model.
        /// </summary>
        public ReadOnlyTableCollection UserViews
        {
            get { return TableCollection.FindByTableType(Tables, Table.TableTypeUserView); }
        }

        /// <summary>
        /// Searches for the table with the specified name.
        /// </summary>
        /// <param name="name">The String containing the name of the table to get.</param>
        /// <returns>A Table object with the specified name, if found; otherwise a null.</returns>
        /// <exception cref="AmbiguousMatchException">More than one table is found with the specified name.</exception>
        public Table this[string name]
        {
            get { return TableCollection.FindByName(Tables, name); }
        }

        /// <summary>
        /// Searches for the table with the specified name.
        /// </summary>
        /// <param name="name">The String containing the name of the table to get.</param>
        /// <returns>A Table object with the specified name, if found; otherwise a null.</returns>
        /// <exception cref="AmbiguousMatchException">More than one table is found with the specified name.</exception>
        public Table Find(string name)
        {
            return TableCollection.FindByName(Tables, name);
        }

        #region Debugger
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "See DebuggerDisplayAttribute marked on this class.")]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string DebuggerDisplay { get { return DatabaseName; } }
        #endregion
    }
}
